package com.colorful.insuranceproject.utils.wheel;

import android.support.annotation.NonNull;
import android.view.View;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.bigkoo.pickerview.lib.WheelView;
import com.bigkoo.pickerview.listener.OnItemSelectedListener;
import com.colorful.insuranceproject.utils.WheelActivity;

import java.util.List;

public class WheelOptions<T> {
    private View view;
    private WheelView wv_option1;
    private WheelView wv_option2;
    private WheelView wv_option3;

    private boolean linkage;
    private OnItemSelectedListener wheelListener_option1;
    private OnItemSelectedListener wheelListener_option2;

    //文字的颜色和分割线的颜色
    int textColorOut;
    int textColorCenter;
    int dividerColor;

    private WheelView.DividerType dividerType;

    // 条目间距倍数
    float lineSpacingMultiplier = 1.6F;
    private List<WheelActivity.Parent> mParents;

    public View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }

    public WheelOptions(View view, Boolean linkage) {
        super();
        this.linkage = linkage;
        this.view = view;
        wv_option1 = (WheelView) view.findViewById(com.bigkoo.pickerview.R.id.options1);// 初始化时显示的数据
        wv_option2 = (WheelView) view.findViewById(com.bigkoo.pickerview.R.id.options2);
        wv_option3 = (WheelView) view.findViewById(com.bigkoo.pickerview.R.id.options3);
    }

    public void setPicker(@NonNull final List<WheelActivity.Parent> parents) {
        this.mParents = parents;

        WheelActivity.Parent initParent = null;
        WheelActivity.Middle initMiddle = null;

        // 选项1
        wv_option1.setAdapter(new ArrayWheelAdapter(parents));// 设置显示数据
        wv_option1.setCurrentItem(0);// 初始化时显示的数据

        if (parents.size() > 0 && parents.get(0).getMiddles() != null) {
            wv_option2.setAdapter(new ArrayWheelAdapter((initParent = parents.get(0)).getMiddles()));// 设置显示数据
            wv_option2.setCurrentItem(wv_option1.getCurrentItem());// 初始化时显示的数据
        }


        // 选项3

        if (initParent != null && initParent.getMiddles() != null && initParent.getMiddles().size() > 0) {
            initMiddle = initParent.getMiddles().get(0);
            wv_option3.setAdapter(new ArrayWheelAdapter(initMiddle.getChildren()));// 设置显示数据
        }

        wv_option3.setCurrentItem(wv_option3.getCurrentItem());
        wv_option1.setIsOptions(true);
        wv_option2.setIsOptions(true);
        wv_option3.setIsOptions(true);

        if (initParent == null)
            wv_option2.setVisibility(View.GONE);
        if (initMiddle == null)
            wv_option3.setVisibility(View.GONE);

        // 联动监听器
        wheelListener_option1 = new OnItemSelectedListener() {

            @Override
            public void onItemSelected(int index) {
                index = index < 0 ? 0 : (index >= parents.size() ? parents.size() - 1 : index);

                List<WheelActivity.Middle> middles = parents.get(index).getMiddles();
                if (middles != null) {
                    if (!middles.isEmpty()) {
                        int oldOpt2SelectPos = wv_option2.getCurrentItem();
                        if (oldOpt2SelectPos < 0) oldOpt2SelectPos = 0;
                        if (oldOpt2SelectPos >= middles.size())
                            oldOpt2SelectPos = middles.size() - 1;

                        wv_option2.setAdapter(new ArrayWheelAdapter(middles));
                        wv_option2.setCurrentItem(oldOpt2SelectPos);

                        int oldOpt3SelectPos = wv_option3.getCurrentItem();
                        List<WheelActivity.Child> children = middles.get(oldOpt2SelectPos).getChildren();
                        if (children != null && !children.isEmpty()) {
                            wv_option3.setAdapter(new ArrayWheelAdapter(children));
                            oldOpt3SelectPos = oldOpt3SelectPos < 0 ? 0 : oldOpt3SelectPos >= children.size() ? children.size() - 1 : oldOpt3SelectPos;
                            wv_option3.setCurrentItem(oldOpt3SelectPos);

                        } else {
                            wv_option3.setAdapter(null);
                        }

                    } else {
                        wv_option2.setAdapter(null);
                        wv_option3.setAdapter(null);
                    }
                }
            }
        };
        wheelListener_option2 = new OnItemSelectedListener() {

            @Override
            public void onItemSelected(int index) {
                if (parents.size() > 0) {
                    int opt1Select = wv_option1.getCurrentItem();

                    //调整一级联动中的位置
                    if (opt1Select < 0) opt1Select = 0;
                    if (opt1Select >= parents.size()) opt1Select = parents.size() - 1;

                    List<WheelActivity.Middle> middles = parents.get(opt1Select).getMiddles();

                    if (middles != null && middles.size() > 0) {
                        //调整二级
                        if (index < 0) index = 0;
                        if (index >= middles.size()) index = middles.size() - 1;

                        int oldSelPos = wv_option3.getCurrentItem();

                        List<WheelActivity.Child> children = middles.get(index).getChildren();

                        if (children != null && children.size() > 0) {
                            oldSelPos = oldSelPos < 0 ? 0 : (oldSelPos >= children.size() ? children.size() - 1 : oldSelPos);
                            wv_option3.setAdapter(new ArrayWheelAdapter(children));
                            wv_option3.setCurrentItem(oldSelPos);
                        } else {
                            wv_option3.setAdapter(null);
                        }
                    }
                }
            }
        };

        // 添加联动监听
        if (initParent != null && linkage)
            wv_option1.setOnItemSelectedListener(wheelListener_option1);

        if (initMiddle != null && linkage)
            wv_option2.setOnItemSelectedListener(wheelListener_option2);
    }

    public void setTextContentSize(int textSize) {
        wv_option1.setTextSize(textSize);
        wv_option2.setTextSize(textSize);
        wv_option3.setTextSize(textSize);
    }

    private void setTextColorOut() {
        wv_option1.setTextColorOut(textColorOut);
        wv_option2.setTextColorOut(textColorOut);
        wv_option3.setTextColorOut(textColorOut);

    }

    private void setTextColorCenter() {
        wv_option1.setTextColorCenter(textColorCenter);
        wv_option2.setTextColorCenter(textColorCenter);
        wv_option3.setTextColorCenter(textColorCenter);

    }

    private void setDividerColor() {
        wv_option1.setDividerColor(dividerColor);
        wv_option2.setDividerColor(dividerColor);
        wv_option3.setDividerColor(dividerColor);
    }

    private void setDividerType() {
        wv_option1.setDividerType(dividerType);
        wv_option2.setDividerType(dividerType);
        wv_option3.setDividerType(dividerType);
    }

    private void setLineSpacingMultiplier() {
        wv_option1.setLineSpacingMultiplier(lineSpacingMultiplier);
        wv_option2.setLineSpacingMultiplier(lineSpacingMultiplier);
        wv_option3.setLineSpacingMultiplier(lineSpacingMultiplier);

    }

    /**
     * 设置选项的单位
     *
     * @param label1 单位
     * @param label2 单位
     * @param label3 单位
     */
    public void setLabels(String label1, String label2, String label3) {
        if (label1 != null)
            wv_option1.setLabel(label1);
        if (label2 != null)
            wv_option2.setLabel(label2);
        if (label3 != null)
            wv_option3.setLabel(label3);
    }

    /**
     * 设置是否循环滚动
     *
     * @param cyclic 是否循环
     */
    public void setCyclic(boolean cyclic) {
        wv_option1.setCyclic(cyclic);
        wv_option2.setCyclic(cyclic);
        wv_option3.setCyclic(cyclic);
    }

    /**
     * 分别设置第一二三级是否循环滚动
     *
     * @param cyclic1,cyclic2,cyclic3 是否循环
     */
    public void setCyclic(boolean cyclic1, boolean cyclic2, boolean cyclic3) {
        wv_option1.setCyclic(cyclic1);
        wv_option2.setCyclic(cyclic2);
        wv_option3.setCyclic(cyclic3);
    }

    /**
     * 返回当前选中的结果对应的位置数组 因为支持三级联动效果，分三个级别索引，0，1，2
     *
     * @return 索引数组
     */
    public int[] getCurrentItems() {
        int[] currentItems = new int[3];
        currentItems[0] = wv_option1.getCurrentItem();
        currentItems[1] = wv_option2.getCurrentItem();
        currentItems[2] = wv_option3.getCurrentItem();
        return currentItems;
    }

    public void setCurrentItems(int option1, int option2, int option3) {
        if (linkage) {
            itemSelected(option1, option2, option3);
        }
        wv_option1.setCurrentItem(option1);
        wv_option2.setCurrentItem(option2);
        wv_option3.setCurrentItem(option3);
    }

    private void itemSelected(int opt1Select, int opt2Select, int opt3Select) {

        if (mParents != null && mParents.size() > opt1Select && opt1Select >= 0) {
            wv_option1.setCurrentItem(opt1Select);

            List<WheelActivity.Middle> middles = mParents.get(opt1Select).getMiddles();
            if (middles != null && middles.size() > opt2Select && opt2Select >= 0) {
                wv_option2.setAdapter(new ArrayWheelAdapter(middles));
                wv_option2.setCurrentItem(opt2Select);

                List<WheelActivity.Child> children = middles.get(opt2Select).getChildren();
                if (children != null && children.size() > opt3Select && opt3Select > 0) {
                    wv_option3.setAdapter(new ArrayWheelAdapter(children));
                    wv_option3.setCurrentItem(opt3Select);
                }

            }
        }
    }

    /**
     * 设置间距倍数,但是只能在1.0-2.0f之间
     *
     * @param lineSpacingMultiplier
     */
    public void setLineSpacingMultiplier(float lineSpacingMultiplier) {
        this.lineSpacingMultiplier = lineSpacingMultiplier;
        setLineSpacingMultiplier();
    }

    /**
     * 设置分割线的颜色
     *
     * @param dividerColor
     */
    public void setDividerColor(int dividerColor) {
        this.dividerColor = dividerColor;
        setDividerColor();
    }

    public List<WheelActivity.Parent> getParents() {
        return mParents;
    }

    /**
     * 设置分割线的类型
     *
     * @param dividerType
     */
    public void setDividerType(WheelView.DividerType dividerType) {
        this.dividerType = dividerType;
        setDividerType();
    }

    /**
     * 设置分割线之间的文字的颜色
     *
     * @param textColorCenter
     */
    public void setTextColorCenter(int textColorCenter) {
        this.textColorCenter = textColorCenter;
        setTextColorCenter();
    }

    /**
     * 设置分割线以外文字的颜色
     *
     * @param textColorOut
     */
    public void setTextColorOut(int textColorOut) {
        this.textColorOut = textColorOut;
        setTextColorOut();
    }
}
